import React from 'react';
import { Card } from 'antd';
import StatefulCalculationCard from '../../../../../../components/common/StatefulCalculationCard';
import NumberField from '../../../../../../components/common/NumberField';

// 倒三角桁架结构计算表单类型定义
interface InvertedTrussStructureFormValues {
  // 桁架参数
  trussLength_load: number;
  trussHeight_load: number;
  trussWidth_load: number;
  topChordDiameter_load: number;
  bottomChordDiameter_load: number;
  webThickness_load: number;
  webDiameter_load: number;
  topChordThickness_load: number;
  bottomChordThickness_load: number;
  steelDensity_load: number;
  verticalSpacing_load: number; // 竖杆间隔 (m)
  safetyFactor_load: number;
  topChordHorizontalDiameter_load: number;
  topChordDiagonalDiameter_load: number;
  topChordHorizontalThickness_load: number;
  topChordDiagonalThickness_load: number;
}

// 倒三角桁架结构计算结果类型定义
interface InvertedTrussStructureResult {
  memberForces: number;
  maxDisplacement: number;
  rigidityRatio: number;
}

// 倒三角桁架自重荷载计算表单类型定义
interface InvertedTrussSelfWeightFormValues {
  // 桁架参数
  length: number;
  height: number;
  width: number;
  topChordDiameter: number;
  bottomChordDiameter: number;
  webThickness: number;
  webDiameter: number;
  topChordThickness: number;
  bottomChordThickness: number;
  steelDensity: number;
  verticalWebSpacing: number; // 竖杆间隔 (m)
  safetyFactor: number;
  topChordHorizontalDiameter: number;
  topChordDiagonalDiameter: number;
  topChordHorizontalThickness: number;
  topChordDiagonalThickness: number;
}

// 倒三角桁架自重荷载计算结果类型定义
interface InvertedTrussSelfWeightResult {
  topChordWeight: number;
  bottomChordWeight: number;
  webWeight: number;
  totalWeight: number;
  linearLoad: number;
}

// 倒三角桁架外部荷载计算表单类型定义
interface InvertedTrussExternalLoadFormValues {
  // 管道参数
  pipeOuterDiameter: number;
  pipeThickness: number;
  pipeDensity: number;
  mediumDensity: number;
  insulationOuterDiameter: number;
  insulationInnerDiameter: number;
  insulationDensity: number;
  // 附属设备参数
  equipmentWeight: number;
  spanLength: number;
  // 环境参数
  windPressure: number;
  windShapeFactor: number;
  windHeightFactor: number;
  windVibrationFactor: number;
  snowDistributionCoefficient: number;
  snowPressure: number;
  openingReductionCoefficient: number;
  projectionAreaOfSectionsPipesWalkways: number;
  totalContourAreaOfCalculationDeck: number;

  maintenanceLoad: number;

  // 温度参数
  elasticModulus: number;
  thermalExpansion: number;
  temperatureDifference: number;
  // 荷载分项系数
  deadLoadFactor: number;
  liveLoadFactor: number;
}

// 倒三角桁架外部荷载计算结果类型定义
interface InvertedTrussExternalLoadResult {
  // 管道荷载
  pipeWeightLoad: number;
  mediumWeightLoad: number;
  insulationWeightLoad: number;
  equipmentWeightLoad: number;
  // 环境荷载
  windLoad: number;
  snowLoad: number;
  maintenanceLoadResult: number;
  // 温度荷载
  temperatureLoad: number;
  // 组合荷载
  deadLoad: number;
  liveLoad: number;
  combinedLoad: number;
}

// 倒三角桁架整体荷载计算表单类型定义
interface InvertedTrussTotalLoadFormValues {
  // 管道荷载
  pipeLoad: number;
  mediumLoad: number;
  insulationLoad: number;
  equipmentLoad: number;
  // 环境荷载
  windLoad: number;
  snowLoad: number;
  maintenanceLoad: number;
  // 温度荷载
  temperatureLoad: number;
  // 荷载分项系数
  deadLoadFactor: number;
  liveLoadFactor: number;
}

// 倒三角桁架整体荷载计算结果类型定义
interface InvertedTrussTotalLoadResult {
  totalLoad: number;
  deadLoad: number;
  liveLoad: number;
  deadFactor: number;
  liveFactor: number;
}

// 倒三角桁架结构计算初始值配置
const structureInitialValues: InvertedTrussStructureFormValues = {
  // 桁架参数
  trussLength_load: 14.0,
  trussHeight_load: 1.0,
  trussWidth_load: 0.8,
  topChordDiameter_load: 0.089,
  bottomChordDiameter_load: 0.089,
  webThickness_load: 0.004,
  webDiameter_load: 0.057,
  topChordThickness_load: 0.006,
  bottomChordThickness_load: 0.006,
  steelDensity_load: 7850,
  verticalSpacing_load: 1.4, // 竖杆间隔 (m)
  safetyFactor_load: 1.2,
  topChordHorizontalDiameter_load: 0.076,
  topChordDiagonalDiameter_load: 0.057,
  topChordHorizontalThickness_load: 0.004,
  topChordDiagonalThickness_load: 0.004,
};

// 倒三角桁架自重荷载计算初始值配置
const selfWeightInitialValues: InvertedTrussSelfWeightFormValues = {
  // 桁架几何参数
  length: 20.0,
  height: 3.0,
  width: 4.0,
  // 杆件参数
  topChordDiameter: 0.159,
  bottomChordDiameter: 0.159,
  webDiameter: 0.089,
  topChordThickness: 0.006,
  bottomChordThickness: 0.006,
  webThickness: 0.005,
  topChordHorizontalDiameter: 0.057,
  topChordDiagonalDiameter: 0.057,
  topChordHorizontalThickness: 0.004,
  topChordDiagonalThickness: 0.004,
  // 材料参数
  steelDensity: 7850,
  // 其他参数
  verticalWebSpacing: 2.0,
  safetyFactor: 1.2,
};

// 倒三角桁架外部荷载计算初始值配置
const externalLoadInitialValues: InvertedTrussExternalLoadFormValues = {
  // 管道参数
  pipeOuterDiameter: 0.325,
  pipeThickness: 0.01,
  pipeDensity: 7850,
  mediumDensity: 900,
  insulationOuterDiameter: 0.4,
  insulationInnerDiameter: 0.325,
  insulationDensity: 80,
  // 附属设备参数
  equipmentWeight: 500,
  spanLength: 14.0,
  // 环境参数
  windPressure: 0.5,
  windShapeFactor: 1.2,
  windHeightFactor: 1.0,
  windVibrationFactor: 1.5,
  snowPressure: 0.3,
  snowDistributionCoefficient: 1.0,
  maintenanceLoad: 1000,
  
  openingReductionCoefficient: 1.0,
  projectionAreaOfSectionsPipesWalkways: 1.0,
  totalContourAreaOfCalculationDeck: 1.0,

  
  // 温度参数
  elasticModulus: 206000,
  thermalExpansion: 0.000012,
  temperatureDifference: 50,
  // 荷载分项系数
  deadLoadFactor: 1.2,
  liveLoadFactor: 1.4,
};

// 倒三角桁架整体荷载计算初始值配置
const totalLoadInitialValues: InvertedTrussTotalLoadFormValues = {
  // 管道荷载
  pipeLoad: 0,
  mediumLoad: 0,
  insulationLoad: 0,
  equipmentLoad: 0,
  // 环境荷载
  windLoad: 0,
  snowLoad: 0,
  maintenanceLoad: 0,
  // 温度荷载
  temperatureLoad: 0,
  // 荷载分项系数
  deadLoadFactor: 1.2,
  liveLoadFactor: 1.4,
};

// 结构计算配置
const structureConfig = {
  key: 'inverted-triangluar-truss-structure',
  label: '倒三角桁架结构计算',
  formKey: 'inverted-triangluar-truss-structure',
  initialValues: structureInitialValues,
  calculationType: 'crossing-inverted-triangular-truss-calculation',
  resultFormatter: (data: InvertedTrussStructureResult) => [
    { key: 'memberForces', name: '杆件内力', value: data.memberForces, unit: 'kN' },
    { key: 'maxDisplacement', name: '最大位移', value: data.maxDisplacement, unit: 'mm' },
    { key: 'rigidityRatio', name: '刚度比', value: typeof data.rigidityRatio === 'number' ? data.rigidityRatio.toFixed(3) : 0, unit: '' }
  ],
  renderFormFields: () => (
    <>
      <NumberField name="trussLength_load" label="桁架长度 (m)" required min={0} step={0.1} />
      <NumberField name="trussHeight_load" label="桁架高度 (m)" required min={0} step={0.1} />
      <NumberField name="trussWidth_load" label="桁架宽度 (m)" required min={0} step={0.1} />
      <NumberField name="topChordHorizontalDiameter_load" label="上弦横杆直径 (m)" required min={0} step={0.001} />
      <NumberField name="topChordDiagonalDiameter_load" label="上弦斜杆直径 (m)" required min={0} step={0.001} />
      <NumberField name="webDiameter_load" label="腹杆直径 (m)" required min={0} step={0.001} />
      <NumberField name="topChordHorizontalThickness_load" label="上弦横杆壁厚 (m)" required min={0} step={0.001} />
      <NumberField name="topChordDiagonalThickness_load" label="上弦斜杆壁厚 (m)" required min={0} step={0.001} />
      <NumberField name="webThickness_load" label="腹杆壁厚 (m)" required min={0} step={0.001} />
      <NumberField name="topChordHorizontalDiameter_load" label="上弦横杆直径 (m)" required min={0} step={0.001} />
      <NumberField name="topChordDiagonalDiameter_load" label="上弦斜杆直径 (m)" required min={0} step={0.001} />
      <NumberField name="topChordHorizontalThickness_load" label="上弦横杆壁厚 (m)" required min={0} step={0.001} />
      <NumberField name="topChordDiagonalThickness_load" label="上弦斜杆壁厚 (m)" required min={0} step={0.001} />
      <NumberField name="steelDensity_load" label="钢材密度 (kg/m³)" required min={0} step={1} />
      <NumberField name="verticalSpacing_load" label="竖杆间隔 (m)" required min={0.1} step={0.1} />
      <NumberField name="safetyFactor_load" label="安全系数" required min={1} step={0.1} />
    </>
  )
};

// 自重荷载计算配置
const selfWeightConfig = {
  key: 'inverted-truss-self-weight',
  label: '倒三角桁架自重荷载',
  formKey: 'inverted-truss-self-weight',
  initialValues: selfWeightInitialValues,
  calculationType: 'crossing-inverted-triangular-truss-load-calculation',
  resultFormatter: (data: InvertedTrussSelfWeightResult) => [
    { key: 'topChordWeight', name: '上弦杆重量', value: data.topChordWeight, unit: 'kg', category: '上弦杆' },
    { key: 'bottomChordWeight', name: '下弦杆重量', value: data.bottomChordWeight, unit: 'kg', category: '下弦杆' },
    { key: 'webWeight', name: '腹杆重量', value: data.webWeight, unit: 'kg', category: '腹杆' },
    { key: 'totalWeight', name: '桁架总重量', value: data.totalWeight, unit: 'kg', category: '总计' },
    { key: 'linearLoad', name: '线荷载', value: data.linearLoad, unit: 'N/m', category: '总计' },
  ],
  renderFormFields: () => (
    <>
      <NumberField name="length" label="桁架长度 (m)" required min={0} step={0.1} />
      <NumberField name="height" label="桁架高度 (m)" required min={0} step={0.1} />
      <NumberField name="width" label="桁架宽度 (m)" required min={0} step={0.1} />
      <NumberField name="topChordDiameter" label="上弦杆直径 (m)" required min={0} step={0.001} />
      <NumberField name="bottomChordDiameter" label="下弦杆直径 (m)" required min={0} step={0.001} />
      <NumberField name="webDiameter" label="腹杆直径 (m)" required min={0} step={0.001} />
      <NumberField name="topChordThickness" label="上弦杆壁厚 (m)" required min={0} step={0.001} />
      <NumberField name="bottomChordThickness" label="下弦杆壁厚 (m)" required min={0} step={0.001} />
      <NumberField name="webThickness" label="腹杆壁厚 (m)" required min={0} step={0.001} />
      <NumberField name="topChordHorizontalDiameter" label="上弦横杆直径 (m)" required min={0} step={0.001} />
      <NumberField name="topChordDiagonalDiameter" label="上弦斜杆直径 (m)" required min={0} step={0.001} />
      <NumberField name="topChordHorizontalThickness" label="上弦横杆壁厚 (m)" required min={0} step={0.001} />
      <NumberField name="topChordDiagonalThickness" label="上弦斜杆壁厚 (m)" required min={0} step={0.001} />
      <NumberField name="steelDensity" label="钢材密度 (kg/m³)" required min={0} step={1} />
      <NumberField name="verticalWebSpacing" label="竖杆间隔 (m)" required min={0.1} step={0.1} />
      <NumberField name="safetyFactor" label="安全系数" required min={1} step={0.1} />
    </>
  )
};

// 外部荷载计算配置
const externalLoadConfig = {
  key: 'inverted-truss-external-load',
  label: '倒三角桁架外部荷载',
  formKey: 'inverted-truss-external-load',
  initialValues: externalLoadInitialValues,
  calculationType: 'crossing-inverted-triangular-truss-external-load-calculation',
  resultFormatter: (data: InvertedTrussExternalLoadResult) => [
    // 管道荷载
    { key: 'pipeWeightLoad', name: '管道自重荷载', value: data.pipeWeightLoad, unit: 'N/m', category: '管道荷载' },
    { key: 'mediumWeightLoad', name: '介质重量荷载', value: data.mediumWeightLoad, unit: 'N/m', category: '管道荷载' },
    { key: 'insulationWeightLoad', name: '保温层重量荷载', value: data.insulationWeightLoad, unit: 'N/m', category: '管道荷载' },
    { key: 'equipmentWeightLoad', name: '附属设备重量荷载', value: data.equipmentWeightLoad, unit: 'N/m', category: '管道荷载' },
    // 环境荷载
    { key: 'windLoad', name: '风荷载', value: data.windLoad, unit: 'N/m', category: '环境荷载' },
    { key: 'snowLoad', name: '雪荷载', value: data.snowLoad, unit: 'N/m', category: '环境荷载' },
    { key: 'maintenanceLoadResult', name: '检修荷载', value: data.maintenanceLoadResult, unit: 'N/m', category: '环境荷载' },
    // 温度荷载
    { key: 'temperatureLoad', name: '温度作用荷载', value: data.temperatureLoad, unit: 'N/m', category: '温度荷载' },
    // 组合荷载
    { key: 'deadLoad', name: '恒载组合值', value: data.deadLoad, unit: 'N/m', category: '组合荷载' },
    { key: 'liveLoad', name: '活载组合值', value: data.liveLoad, unit: 'N/m', category: '组合荷载' },
    { key: 'combinedLoad', name: '组合设计荷载', value: data.combinedLoad, unit: 'N/m', category: '组合荷载' },
  ],
  renderFormFields: () => (
    <>
      {/* 管道参数标签页 */}
      <div style={{ marginBottom: '16px' }}>
        <h4>管道参数</h4>
        <NumberField name="pipeOuterDiameter" label="管道外径 (m)" required min={0} step={0.001} />
        <NumberField name="pipeThickness" label="管道壁厚 (m)" required min={0} step={0.001} />
        <NumberField name="pipeDensity" label="管道密度 (kg/m³)" required min={0} step={1} />
        <NumberField name="mediumDensity" label="介质密度 (kg/m³)" required min={0} step={1} />
        <NumberField name="insulationOuterDiameter" label="保温层外径 (m)" required min={0} step={0.001} />
        <NumberField name="insulationInnerDiameter" label="保温层内径 (m)" required min={0} step={0.001} />
        <NumberField name="insulationDensity" label="保温层密度 (kg/m³)" required min={0} step={1} />
        <NumberField name="equipmentWeight" label="附属设备总重量 (kg)" required min={0} step={1} />
        <NumberField name="spanLength" label="跨度长度 (m)" required min={0} step={0.1} />
      </div>
      
      {/* 环境参数标签页 */}
      <div style={{ marginBottom: '16px' }}>
        <h4>环境参数</h4>
        <NumberField name="windPressure" label="基本风压 (kN/m²)" required min={0} step={0.01} />
        <NumberField name="windShapeFactor" label="风荷载体型系数" required min={0} step={0.01} />
        <NumberField name="windHeightFactor" label="风压高度变化系数" required min={0} step={0.01} />
        <NumberField name="windVibrationFactor" label="风振系数" required min={0} step={0.01} />
        <NumberField name="snowPressure" label="基本雪压 (kN/m²)" required min={0} step={0.01} />
        <NumberField name="snowDistributionCoefficient" label="屋面积雪分布系数" required min={0} step={0.01} />
        <NumberField name="maintenanceLoad" label="检修荷载 (N/m)" required min={0} step={10} />
        <NumberField name="elasticModulus" label="弹性模量 (MPa)" required min={0} step={1000} />
        <NumberField name="thermalExpansion" label="线膨胀系数 (1/°C)" required min={0} step={0.000001} />
        <NumberField name="temperatureDifference" label="温度变化值 (°C)" required min={0} step={1} />
      </div>
      
      {/* 荷载分项系数标签页 */}
      <div style={{ marginBottom: '16px' }}>
        <h4>荷载分项系数</h4>
        <NumberField name="deadLoadFactor" label="恒载分项系数" required min={0} step={0.01} />
        <NumberField name="liveLoadFactor" label="活载分项系数" required min={0} step={0.01} />
      </div>
    </>
  )
};

// 整体荷载计算配置
const totalLoadConfig = {
  key: 'inverted-truss-total-load',
  label: '倒三角桁架整体荷载',
  formKey: 'inverted-truss-total-load',
  initialValues: totalLoadInitialValues,
  calculationType: 'combinedLoad',
  resultFormatter: (data: InvertedTrussTotalLoadResult) => [
    { key: 'totalLoad', name: '总荷载', value: data.totalLoad, unit: 'N/m' },
    { key: 'deadLoad', name: '恒载组合值', value: data.deadLoad, unit: 'N/m' },
    { key: 'liveLoad', name: '活载组合值', value: data.liveLoad, unit: 'N/m' },
    { key: 'deadFactor', name: '恒载分项系数', value: data.deadFactor, unit: '' },
    { key: 'liveFactor', name: '活载分项系数', value: data.liveFactor, unit: '' },
  ],
  renderFormFields: () => (
    <>
      {/* 管道荷载参数标签页 */}
      <div style={{ marginBottom: '16px' }}>
        <h4>管道荷载</h4>
        <NumberField name="pipeLoad" label="管道自重荷载 (N/m)" required min={0} step={1} />
        <NumberField name="mediumLoad" label="介质重量荷载 (N/m)" required min={0} step={1} />
        <NumberField name="insulationLoad" label="保温层重量荷载 (N/m)" required min={0} step={1} />
        <NumberField name="equipmentLoad" label="附属设备重量荷载 (N/m)" required min={0} step={1} />
      </div>
      
      {/* 环境荷载参数标签页 */}
      <div style={{ marginBottom: '16px' }}>
        <h4>环境荷载</h4>
        <NumberField name="windLoad" label="风荷载 (N/m)" required min={0} step={1} />
        <NumberField name="snowLoad" label="雪荷载 (N/m)" required min={0} step={1} />
        <NumberField name="maintenanceLoad" label="检修荷载 (N/m)" required min={0} step={10} />
        <NumberField name="temperatureLoad" label="温度作用荷载 (N/m)" required min={0} step={1} />
      </div>
      
      {/* 荷载分项系数标签页 */}
      <div style={{ marginBottom: '16px' }}>
        <h4>荷载分项系数</h4>
        <NumberField name="deadLoadFactor" label="恒载分项系数" required min={0} step={0.01} />
        <NumberField name="liveLoadFactor" label="活载分项系数" required min={0} step={0.01} />
      </div>
    </>
  )
};

// 创建四个独立的计算卡片
const InvertedTrussStructureCard: React.FC = () => (
  <StatefulCalculationCard
    title={structureConfig.label}
    formKey={structureConfig.formKey}
    initialValues={structureConfig.initialValues}
    renderFormFields={structureConfig.renderFormFields}
    calculationType={structureConfig.calculationType}
    resultFormatter={structureConfig.resultFormatter}
    columnCount={2}
    tableHeight={300}
  />
);

const InvertedTrussSelfWeightCard: React.FC = () => (
  <StatefulCalculationCard
    title={selfWeightConfig.label}
    formKey={selfWeightConfig.formKey}
    initialValues={selfWeightConfig.initialValues}
    renderFormFields={selfWeightConfig.renderFormFields}
    calculationType={selfWeightConfig.calculationType}
    resultFormatter={selfWeightConfig.resultFormatter}
    columnCount={2}
    tableHeight={300}
  />
);

const InvertedTrussExternalLoadCard: React.FC = () => (
  <StatefulCalculationCard
    title={externalLoadConfig.label}
    formKey={externalLoadConfig.formKey}
    initialValues={externalLoadConfig.initialValues}
    renderFormFields={externalLoadConfig.renderFormFields}
    calculationType={externalLoadConfig.calculationType}
    resultFormatter={externalLoadConfig.resultFormatter}
    columnCount={2}
    tableHeight={300}
  />
);

const InvertedTrussTotalLoadCard: React.FC = () => (
  <StatefulCalculationCard
    title={totalLoadConfig.label}
    formKey={totalLoadConfig.formKey}
    initialValues={totalLoadConfig.initialValues}
    renderFormFields={totalLoadConfig.renderFormFields}
    calculationType={totalLoadConfig.calculationType}
    resultFormatter={totalLoadConfig.resultFormatter}
    columnCount={2}
    tableHeight={300}
  />
);

// 包含四个计算卡片的组件
const InvertedTrussMultiCard: React.FC = () => {
  return (
    <Card title="倒三角桁架计算">
      <div style={{ display: 'flex', flexDirection: 'column' }}>
        <div>
          <InvertedTrussStructureCard />
        </div>
        <div style={{ 
          height: '1px', 
          backgroundColor: '#f0f0f0', 
          margin: '24px 0' 
        }}></div>
        <div>
          <InvertedTrussSelfWeightCard />
        </div>
        <div style={{ 
          height: '1px', 
          backgroundColor: '#f0f0f0', 
          margin: '24px 0' 
        }}></div>
        <div>
          <InvertedTrussExternalLoadCard />
        </div>
        <div style={{ 
          height: '1px', 
          backgroundColor: '#f0f0f0', 
          margin: '24px 0' 
        }}></div>
        <div>
          <InvertedTrussTotalLoadCard />
        </div>
      </div>
    </Card>
  );
};

export default InvertedTrussMultiCard;